<template>
  <!-- 登录页面 -->
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration"></div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="login-header">
        <h2 class="login-title">用户登录</h2>
        <p class="login-subtitle">请输入账号信息完成登录</p>
      </div>

      <form class="login-form" @submit.prevent="login">
        <!-- 简化后的用户类型选择 -->
        <div class="form-group user-type-group">
          <label class="form-label">用户类型</label>
          <div class="radio-group">
            <label
                v-for="type in userTypes"
                :key="type.value"
                class="radio-option"
                :class="{ active: userInfo.userType === type.value }"
            >
              <input
                  v-model="userInfo.userType"
                  type="radio"
                  :value="type.value"
                  class="radio-input"
              >
              <span class="radio-custom"></span>
              <span class="radio-text">{{ type.label }}</span>
            </label>
          </div>
        </div>

        <!-- 手机号输入 -->
        <div class="form-group">
          <label class="form-label">手机号</label>
          <div class="input-wrapper">
            <i class="icon-mobile input-icon"></i>
            <input
                v-model="userInfo.mobile"
                type="tel"
                class="form-input"
                placeholder="请输入手机号"
                required
            >
          </div>
        </div>

        <!-- 密码输入 -->
        <div class="form-group">
          <label class="form-label">密码</label>
          <div class="input-wrapper">
            <i class="icon-lock input-icon"></i>
            <input
                v-model="userInfo.password"
                type="password"
                class="form-input"
                placeholder="请输入密码"
                required
            >
          </div>
        </div>

        <!-- 登录按钮 -->
        <button
            type="submit"
            class="login-btn"
            :class="{ disabled: !isValid }"
            :disabled="!isValid"
        >
          立即登录
          <i class="icon-arrow-right"></i>
        </button>

        <!-- 辅助链接 -->
        <div class="form-helper">
          <a href="/forgot-password" class="helper-link">忘记密码？</a>
          <a href="/register" class="helper-link">还没有账号？立即注册</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import cookie from 'js-cookie'

export default {
  data() {
    return {
      userTypes: [
        { value: 1, label: '投资人' },
        { value: 2, label: '借款人' }
      ],
      userInfo: {
        userType: 1,
        mobile: '',
        password: ''
      },
      isValid: true
    }
  },

  watch: {
    // 实时表单校验
    'userInfo.mobile'(val) {
      this.validateForm()
    },
    'userInfo.password'(val) {
      this.validateForm()
    }
  },

  methods: {
    // 表单校验
    validateForm() {
      const mobileReg = /^1[3-9]\d{9}$/
      const hasMobile = mobileReg.test(this.userInfo.mobile)
      const hasPassword = this.userInfo.password.length >= 6
      this.isValid = hasMobile && hasPassword
    },

    // 登录逻辑
    login() {
      // 再次校验表单
      this.validateForm()
      if (!this.isValid) return

      // 显示加载状态
      const loginBtn = document.querySelector('.login-btn')
      loginBtn.innerHTML = '<i class="icon-loading"></i> 登录中...'
      loginBtn.disabled = true

      this.$axios
          .$post('/api/core/userInfo/login', this.userInfo)
          .then((response) => {
            console.log(response)
            cookie.set('userInfo', JSON.stringify(response.data.userInfo))
            // 登录成功动画跳转
            document.querySelector('.login-card').classList.add('login-success')
            setTimeout(() => {
              window.location.href = '/user'
            }, 600)
          })
          .catch((error) => {
            console.error(error)
            // 错误提示
            alert('登录失败，请检查账号密码')
            // 恢复按钮状态
            loginBtn.innerHTML = '立即登录 <i class="icon-arrow-right"></i>'
            loginBtn.disabled = false
          })
    }
  }
}
</script>

<style scoped>
/* 基础样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 登录容器 */
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: relative;
  background-color: #f5f7fa;
}

/* 背景装饰 */
.bg-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;
}

.bg-decoration::before,
.bg-decoration::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, #4361ee, #3a0ca3);
  filter: blur(100px);
  opacity: 0.2;
}

.bg-decoration::before {
  width: 600px;
  height: 600px;
  top: -300px;
  left: -100px;
  animation: float 15s infinite ease-in-out;
}

.bg-decoration::after {
  width: 500px;
  height: 500px;
  bottom: -250px;
  right: -150px;
  animation: float 20s infinite ease-in-out 2s;
}

/* 登录卡片 */
.login-card {
  width: 100%;
  max-width: 420px;
  background-color: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  padding: 40px 30px;
  position: relative;
  z-index: 1;
  transition: all 0.5s ease;
}

/* 登录头部 */
.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: #1d2129;
  margin-bottom: 8px;
  position: relative;
  display: inline-block;
}

.login-title::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 3px;
  background: linear-gradient(90deg, #4361ee, #3a0ca3);
  border-radius: 3px;
}

.login-subtitle {
  color: #86909c;
  font-size: 14px;
}

/* 表单样式 */
.login-form {
  width: 100%;
}

.form-group {
  margin-bottom: 22px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #4e5969;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
}

.form-input {
  width: 100%;
  height: 48px;
  padding: 0 15px 0 45px;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  font-size: 15px;
  color: #1d2129;
  background-color: #fff;
  transition: all 0.3s ease;
}

.form-input::placeholder {
  color: #c9cdD4;
}

.form-input:focus {
  outline: none;
  border-color: #4361ee;
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.1);
}

.input-icon {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #86909c;
  font-size: 18px;
  transition: color 0.3s ease;
}

.form-input:focus + .input-icon {
  color: #4361ee;
}

/* 用户类型选择 */
.user-type-group {
  margin-bottom: 25px;
}

.radio-group {
  display: flex;
  gap: 20px;
}

.radio-option {
  display: flex;
  align-items: center;
  cursor: pointer;
  position: relative;
  padding-left: 28px;
  color: #4e5969;
  font-size: 15px;
  transition: color 0.3s ease;
}

.radio-option.active {
  color: #4361ee;
}

.radio-input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.radio-custom {
  position: absolute;
  left: 0;
  top: 2px;
  width: 20px;
  height: 20px;
  border: 2px solid #dcdfe6;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.radio-option.active .radio-custom {
  border-color: #4361ee;
}

.radio-custom::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #4361ee;
  transition: transform 0.3s ease;
}

.radio-option.active .radio-custom::after {
  transform: translate(-50%, -50%) scale(1);
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 50px;
  background: linear-gradient(90deg, #4361ee, #3a0ca3);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
  margin-bottom: 20px;
}

.login-btn:hover:not(.disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(67, 97, 238, 0.2);
}

.login-btn:active:not(.disabled) {
  transform: translateY(0);
  box-shadow: 0 5px 10px rgba(67, 97, 238, 0.2);
}

.login-btn.disabled {
  background: #c9cdD4;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* 辅助链接 */
.form-helper {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.helper-link {
  color: #4361ee;
  text-decoration: none;
  transition: all 0.3s ease;
}

.helper-link:hover {
  color: #3a0ca3;
  text-decoration: underline;
}

/* 动画效果 */
@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-20px);
  }
}

/* 登录成功动画 */
.login-success {
  animation: loginSuccess 0.6s ease forwards;
}

@keyframes loginSuccess {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.05);
    opacity: 0.8;
  }
  100% {
    transform: scale(0.8);
    opacity: 0;
  }
}

/* 图标样式（使用伪元素模拟图标） */
.icon-mobile::before { content: '📱'; }
.icon-lock::before { content: '🔒'; }
.icon-arrow-right::before { content: '→'; }
.icon-loading::before {
  content: '⟳';
  display: inline-block;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 响应式调整 */
@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }

  .form-group {
    margin-bottom: 18px;
  }

  .login-btn {
    height: 45px;
    font-size: 15px;
  }
}
</style>